package com.cn.esermis.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cn.esermis.dpld.entity.PostMarking;
import com.cn.esermis.dpld.mapper.DataAcquisitionInstrumentMapper;
import com.cn.esermis.dpld.model.PostMarkingVo;
import com.cn.esermis.dpld.service.IDataAcquisitionInstrumentService;
import com.cn.esermis.dpld.service.IPostMarkingService;
import com.cn.esermis.model.BaseUser;
import com.cn.esermis.utils.TokenUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.cn.esermis.dpld.model.AjaxResult.error;
import static com.cn.esermis.dpld.model.AjaxResult.success;
import static com.cn.esermis.utils.TokenUtils.extractToken;

/**
 * 事后标记
 */
@RestController
@RequestMapping("/post_marking")
public class PostMarkingController
{
    @Resource
    private IPostMarkingService postMarkingService;
    @Resource
    private IDataAcquisitionInstrumentService dataAcquisitionInstrumentService;
    @Resource
    private DataAcquisitionInstrumentMapper dataAcquisitionInstrumentMapper;
    /**
     * 分页查询列表
     * @param vo
     * @return
     */
    @GetMapping("/list")
    public Object list(PostMarkingVo vo, HttpServletRequest request)
    {
        IPage<PostMarking> page = new Page<>(vo.getPageNum(), vo.getPageSize());
        BaseUser userInfo = TokenUtils.getUserInfo(extractToken(request));
        QueryWrapper<PostMarking> objectQueryWrapper = new QueryWrapper<>();
        String verifyCompany = dataAcquisitionInstrumentMapper.verifyCompany(userInfo.getCompanyId());
//        if (vo.getCompanyName() != null) {
            String companyId = userInfo.getCompanyId();
            if (verifyCompany != null && verifyCompany.length() < 5) {
                List<String> companyCodes = dataAcquisitionInstrumentService.getComponyListAll(vo.getCompanyName());
                if (!companyCodes.isEmpty()) {
                    objectQueryWrapper.in("company_code", companyCodes);
                }
            }else{
                List<String> companyCodes = dataAcquisitionInstrumentService.getComponyList(vo.getCompanyName(), companyId);
                if (!companyCodes.isEmpty()) {
                    objectQueryWrapper.in("company_code", companyCodes);
                }
            }
//        }
        objectQueryWrapper.last("ORDER BY FIELD(unit_type, 3,0,1,2)");
        IPage<PostMarking> rpage = postMarkingService.page(page, objectQueryWrapper);
        IPage<PostMarkingVo> voIPage = new Page<>();
        List<PostMarking> records = rpage.getRecords();
        List<PostMarkingVo> recordsvo = new ArrayList<>();
        // 查询企业
        Map<String, Object> componyMap = dataAcquisitionInstrumentService.getComponyMap();
        // 查询工厂
        Map<String, Object> departmentMap = dataAcquisitionInstrumentService.getDepartmentMap();
        records.forEach(x->{
            PostMarkingVo postMarkingVo = new PostMarkingVo();
            BeanUtils.copyProperties(x, postMarkingVo);
            if (postMarkingVo.getCompanyCode() != null && componyMap.get(postMarkingVo.getCompanyCode()) != null) {
                Map componyMapobj = (Map) componyMap.get(postMarkingVo.getCompanyCode());
                postMarkingVo.setCompanyName(componyMapobj.get("companylongname").toString());
            }
            if (postMarkingVo.getDepartmentCode() != null && departmentMap.get(postMarkingVo.getDepartmentCode()) != null) {
                Map componyMapobj = (Map) departmentMap.get(postMarkingVo.getDepartmentCode());
                postMarkingVo.setDepartmentName(componyMapobj.get("departmentname").toString());
            }
            recordsvo.add(postMarkingVo);
        });
        voIPage.setRecords(recordsvo);
        voIPage.setCurrent(rpage.getCurrent());
        voIPage.setSize(rpage.getSize());
        voIPage.setTotal(rpage.getTotal());
        voIPage.setPages(rpage.getPages());
        return success(voIPage);
    }

    /**
     * 查询详情
     * @param id
     * @return
     */
    @GetMapping("/getInfo")
    public Object getInfo(String id)
    {
        return success(postMarkingService.getById(id));
    }


    /**
     * 新增
     * @param postMarking
     * @return
     */
    @PostMapping("/add")
    public Object add(@RequestBody PostMarking postMarking)
    {
        postMarkingService.save(postMarking);
        return success();
    }


    /**&
     * 修改
     * @param postMarking
     * @return
     */
    @PostMapping("/edit")
    public Object edit(@RequestBody PostMarking postMarking)
    {
        // 审核流程控制
        if (postMarking.getVerificationStatus() !=null && postMarking.getReasonClassification()!=null && postMarking.getUnitType() == 0){
            postMarking.setUnitType(1);
        }
        if (postMarking.getUnitType() == 3 && postMarking.getUnitOpinion()==null){
            return error("请填写驳回原因。");
        }
        postMarkingService.updateById(postMarking);
        return success();
    }

    @GetMapping("/del")
    public Object del(String id)
    {
        postMarkingService.removeById(id);
        return success();
    }



}
